package com.nulldev.util.crypto;

import com.nulldev.util.encoding.base64.Base64;
import com.nulldev.util.java.utils.rt.Warning;
import com.nulldev.util.logging.Logger;
import com.nulldev.util.logging.LoggerFactory;
import com.nulldev.util.other.CharsetUtil;

@Deprecated 
@Warning("Outdated API, you should be using cryptoAPI 2x.")
/**
 * No longer used since API v2.0 (mid/end 2017), replaced by cryptoAPI2.
 * @author null
 * @removal This class will be removed in nullUtil 2.7
 */
public class CryptoUtil {
	private static final Logger log = LoggerFactory.getLogger(true, true);
	private static void handle() {
		if (CryptoUtil.class.isAnnotationPresent(Warning.class)) {
			log.warn(CryptoUtil.class.getAnnotation(Warning.class).value());
		}
	}
	/**
	 * Crypto API I (1.0)
	 * @author null
	 *
	 */
	public static class XORUtil{

	    public static String xorMessage(String message, String key) {
	    	handle();
	    	log.warn("xorMessage(a, b) is deprecated in CryptoUtil 1.0.");
	        try {
	            if (message == null || key == null) return null;
	            char[] keys = key.toCharArray();
	            char[] mesg = message.toCharArray();
	            int ml = mesg.length;
	            int kl = keys.length;
	            char[] newmsg = new char[ml];
	            for (int i = 0; i < ml; i++) {
	                newmsg[i] = (char)(mesg[i] ^ keys[i % kl]);
	            }
	            return new String(newmsg);
	        } catch (Exception e) {
	        	e.printStackTrace();
	            return null;
	        }
	    }

	}

	/**
	 * Crypto API II (2.0 - 2.4)
	 * @author null
	 *
	 */
	public static class AdvXORUtil{

	    public static String encode(String s, String key) {
	    	handle();
	    	log.warn("encode(a, b) is deprecated in CryptoUtil 1.0.");
	        return base64Encode(xorWithKey(s.getBytes(CharsetUtil.getCharset("UTF-8")), key.getBytes()));
	    }

	    public static String decode(String s, String key) {
	    	handle();
	    	log.warn("decode(a, b) is deprecated in CryptoUtil 1.0.");
	        return new String(xorWithKey(base64Decode(s), key.getBytes()));
	    }

	    public static String encodeRAW(String s, String key) {
	    	handle();
	    	log.warn("encodeRAW(a, b) is deprecated in CryptoUtil 1.0.");
	        return new String(xorWithKey(s.getBytes(CharsetUtil.getCharset("UTF-8")), key.getBytes()), CharsetUtil.getCharset());
	    }

	    public static String decodeRAW(String s, String key) {
	    	handle();
	    	log.warn("decodeRAW(a, b) is deprecated in CryptoUtil 1.0.");
	        return new String(xorWithKey(s.getBytes(CharsetUtil.getCharset("UTF-8")), key.getBytes()), CharsetUtil.getCharset());
	    }

	    private static byte[] xorWithKey(byte[] a, byte[] key) {
	    	handle();
	        byte[] out = new byte[a.length];
	        for (int i = 0; i < a.length; i++) {
	            out[i] = (byte) (a[i] ^ key[i%key.length]);
	        }
	        return out;
	    }

	    private static byte[] base64Decode(final String s) {
	    	handle();
	    	return Base64.encodeBase64(s.getBytes(CharsetUtil.getCharset()));
	    	//throw new UnsupportedOperationException("Unsupported Java release.");
	    	//try {
	    		//return java.util.Base64.getDecoder().decode(s.getBytes(CharsetUtil.getCharset()));
	    	//}catch (IllegalArgumentException e) {
	    	//	throw new RuntimeException("[CryptoUtil] Failed to decode '" + s + "'. [" + e.getClass().getName() + " -> " + e.getMessage() + "]");
	    	//}
	    }

	    private static String base64Encode(final byte[] bytes) {
	    	handle();
	    	return new String(Base64.decodeBase64(bytes), CharsetUtil.getCharset());
	    	//throw new UnsupportedOperationException("Unsupported Java release.");
			//byte[] new_string = java.util.Base64.getEncoder().encode(bytes);
			//return new String(new_string, CharsetUtil.getCharset());
	    }

	}
}
